package com.twu.biblioteca;

import java.util.ArrayList;

public class Librarian {

     Register register;


    public Librarian(Register registerObj)
    {
        this.register=registerObj;
    }


    public String getBooks() {

        ArrayList<BookRecord> availList=new ArrayList<BookRecord>();
        for(BookRecord bookRecord : register.getBookList())
        {
            if(bookRecord.getNoOfBook()>0)
            {
                availList.add(bookRecord);
            }
        }


        return Formatter.ListFormatter(availList);
    }
    public String getMovies() {

        ArrayList<MovieRecord> availList=new ArrayList<MovieRecord>();
        for(MovieRecord movieRecord : register.getMovieList())
        {
            if(movieRecord.getNoOfCopies()>0)
            {
                availList.add(movieRecord);
            }
        }

        return Formatter.movieListFormatter(availList);
    }




    public Book checkOutBook(Customer customer,String isbn)
    {
        for(BookRecord bookRecord :register.getBookList())
        {
            if(bookRecord.getBook().getIsbn().equalsIgnoreCase(isbn) && bookRecord.getNoOfBook()>0)
            {
                bookRecord.checkOutBook();
                customer.addBook(bookRecord.getBook());
                return bookRecord.getBook();
            }
        }
        return null;

    }

    public Movie checkOutMovie(Customer customer,String name)
    {
        for(MovieRecord movieRecord :register.getMovieList())
        {
            if(movieRecord.getMovie().getName().equalsIgnoreCase(name) && movieRecord.getNoOfCopies()>0)
            {
                movieRecord.checkOutMovie();
                customer.addMovie(movieRecord.getMovie());
                return movieRecord.getMovie();
            }
        }
        return null;

    }


    public boolean searchBook(String isbn)
    {
        boolean flag=false;
        for(BookRecord bookRecord :register.getBookList())
        {
            if(bookRecord.getBook().getIsbn().equalsIgnoreCase(isbn) && bookRecord.getNoOfBook()>0)
            {
                flag=true;
                break;
            }
        }

        return flag;
    }


    public boolean searchMovie(String name)
    {
        boolean flag=false;
        for(MovieRecord movieRecord :register.getMovieList())
        {
            if(movieRecord.getMovie().getName().equalsIgnoreCase(name) && movieRecord.getNoOfCopies()>0)
            {
                flag=true;
                break;
            }
        }

        return flag;
    }


    public boolean searchAllMovie(String name)
    {
        boolean flag=false;
        for(MovieRecord movieRecord :register.getMovieList())
        {
            if(movieRecord.getMovie().getName().equalsIgnoreCase(name))
            {
                flag=true;
                break;
            }
        }

        return flag;
    }


    public boolean searchAllBook(String isbn)
    {
        boolean flag=false;
        for(BookRecord bookRecord :register.getBookList())
        {
            if(bookRecord.getBook().getIsbn().equalsIgnoreCase(isbn))
            {
                flag=true;
                break;
            }
        }

        return flag;
    }

    public boolean returnBook(Customer customer, String isbn) {

        for(BookRecord bookRecord :register.getBookList())
        {
            if(bookRecord.getBook().getIsbn().equalsIgnoreCase(isbn) && customer.searchBook(isbn))
            {
                bookRecord.returnBook();
                customer.removeBook(isbn);
                return true;
            }
        }
        return false;
    }
    public Boolean returnMovie(Customer customer, String name) {

        for(MovieRecord movieRecord :register.getMovieList())
        {
            if(movieRecord.getMovie().getName().equalsIgnoreCase(name) && customer.searchMovie(name))
            {
                movieRecord.returnMovie();
                customer.removeMovie(name);
                return true;
            }
        }
        return false;
    }
}
